<template>
    <div class="page">
        {{dictMap}}
        <el-row style="padding-bottom: 10px;">
            <el-col :span="6">
                <el-input placeholder="关键字搜索">
                    <el-button slot="append" icon="el-icon-search"></el-button>
                </el-input>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24" style="padding: 10px 0px 10px 0px;text-align: right;border-top: 1px solid #e4e7ed;">
                <el-button type="primary" icon="el-icon-edit-outline" @click="openAddDrawer">添加</el-button>
                <el-button type="primary" icon="el-icon-edit" @click="openUpdateDrawer"
                    :disabled="selectRows.length!==1">编辑</el-button>
                <el-button type="primary" icon="el-icon-info" @click="openUpdateDrawer"
                    :disabled="selectRows.length!==1">详情</el-button>
                <el-button type="danger" icon="el-icon-delete" @click="onDelete" :disabled="selectRows.length===0">删除
                </el-button>
            </el-col>
            <el-col :span="24" style="padding-bottom: 10px;">
                <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column fixed prop="memberNo" label="会员编号" width="200"></el-table-column>
                    <el-table-column prop="memberName" label="姓名" width="200"></el-table-column>
                    <el-table-column prop="memberSex" label="性别" width="120"></el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24" style="text-align: center;">
                <el-pagination background layout="total,prev, pager, next" :total="paginationCofig.total"
                    @size-change="sizeChange" :pageSize="paginationCofig.pageSize"
                    :currentPage.sync="paginationCofig.currentPage" @current-change="currentChange"
                    @prev-click="prevClick" @next-click="nextClick"></el-pagination>
            </el-col>
        </el-row>

        <!-- 新增 -->
        <el-drawer title="新增" :visible.sync="isAddDrawer" direction="rtl" :wrapperClosable="false"
            :close-on-press-escape="false" :show-close="false">
            <el-row style="height: calc(100% - 42px);">
                <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px">
                    <el-form-item label="会员名称" prop="memberName">
                        <el-input v-model="addForm.memberName" placeholder="会员名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="会员性别" prop="memberSex">
                        <el-select v-model="addForm.memberSex" placeholder="会员性别" style="width: 80%;">
                            <el-option label="女" value="0"></el-option>
                            <el-option label="男" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row style="height: 42px;text-align: right;border-top: 1px solid #e4e7ed;padding-top: 10px;">
                <el-button size="small" type="primary" icon="el-icon-check" @click="addSave">保存</el-button>
                <el-button size="small" icon="el-icon-refresh-right" @click="resetForm('addForm')">重置</el-button>
                <el-button size="small" type="danger" icon="el-icon-close" @click="isAddDrawer=false">关闭</el-button>
            </el-row>
        </el-drawer>
        <!-- 编辑 -->
        <el-drawer title="编辑" :visible.sync="isUpdateDrawer" direction="rtl" :wrapperClosable="false"
            :close-on-press-escape="false" :show-close="false">
            <el-row style="height: calc(100% - 42px);">
                <el-form :model="updateForm" :rules="rules" ref="updateForm" label-width="100px">
                    <el-form-item label="会员名称" prop="memberName">
                        <el-input v-model="updateForm.memberName" placeholder="会员名称" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="会员性别" prop="memberSex">
                        <el-select v-model="updateForm.memberSex" placeholder="会员性别" style="width: 80%;">
                            <el-option label="女" value="0"></el-option>
                            <el-option label="男" value="1"></el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
            </el-row>
            <el-row style="height: 42px;text-align: right;border-top: 1px solid #e4e7ed;padding-top: 10px;">
                <el-button size="small" type="primary" icon="el-icon-check" @click="updateSave">保存</el-button>
                <el-button size="small" icon="el-icon-refresh-right" @click="resetForm('updateForm')">重置</el-button>
                <el-button size="small" type="danger" icon="el-icon-close" @click="isUpdateDrawer=false">关闭</el-button>
            </el-row>
        </el-drawer>
        <!-- 详情 -->
        <el-drawer title="详情" :visible.sync="isDetailDrawer" direction="rtl">
            <span>我来啦!</span>
        </el-drawer>

    </div>
</template>

<script>
    import _ from 'lodash'
    import $api from '@/api/member'
    import { mapGetters } from 'vuex'

    const formObj = {
        memberName: '',
        memberSex: '',
    }

    export default {
        computed:{
            ...mapGetters([
                'dictMap'
            ])
        },
        data() {
            return {
                /* 分页tiao */
                paginationCofig: {
                    pageSize: 10,
                    currentPage: 1,
                    total: 0,
                },
                /* 表格data */
                tableData: [],
                selectRows: [],
                /* 抽屉 */
                isAddDrawer: false,
                isUpdateDrawer: false,
                isDetailDrawer: false,

                /* 表单 */
                addForm: null,
                updateForm: null,
                rules: {
                    memberName: [
                        { required: true, message: '请输入会员姓名', trigger: 'blur' },
                        { min: 3, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
                    ],
                    memberSex: [
                        { required: true, message: '请选择会员性别', trigger: 'change' }
                    ],
                }
            };
        },
        methods: {
            handleSelectionChange(val) {
                this.selectRows = val
            },
            onDelete() {
                const deleteIds = this.selectRows.map(item => {
                    return item.memberNo
                }).join(",")
                $api.deleteMembers(deleteIds).then(data => {
                    this.$message({
                        message: '删除成功',
                        type: 'success'
                    });
                    this.memberListPage()
                })
            },
            /* 新增抽屉 */
            openAddDrawer() {
                this.isAddDrawer = true
                this.resetForm("addForm")

            },
            addSave() {
                this.validateForm("addForm", (success, errorObj) => {
                    if (success) {
                        const formData = this.addForm
                        $api.addNewMember(formData).then(data => {
                            this.$message({
                                message: '新增成功',
                                type: 'success'
                            });
                            this.isAddDrawer = false
                            this.memberListPage()
                        })
                    }
                })
            },
            /* 修改抽屉 */
            openUpdateDrawer() {
                this.isUpdateDrawer = true
            },
            updateSave() {
                this.validateForm("updateForm", (success, errorObj) => {
                    if (success) {
                        const memberNo = this.selectRows[0].memberNo
                        const formData = this.updateForm
                        $api.updateMember(memberNo, formData).then(data => {
                            this.$message({
                                message: '修改成功',
                                type: 'success'
                            });
                            this.isUpdateDrawer = false
                            this.memberListPage()
                        })
                    }
                })
            },
            /* 详情抽屉 */
            openDetailDrawer() {
                this.isDetailDrawer = true
            },

            /* 分页条-start */
            sizeChange(pageSize) {
                this.memberListPage()
            },
            currentChange(currentPage) {
                this.memberListPage()
            },
            prevClick(pageSize) {
                this.memberListPage()
            },
            nextClick(currentPage) {
                this.memberListPage()
            },
            /* api */
            memberListPage(query) {
                const currentPage = this.paginationCofig.currentPage
                const pageSize = this.paginationCofig.pageSize
                $api.memberListPage(currentPage, pageSize, query).then(data => {
                    // console.log(data.data);
                    this.paginationCofig.total = data.data.total

                    this.tableData = data.data.list
                })

            },

            /* common methods */
            resetForm(formName) {
                this.$refs[formName].resetFields();
            },
            validateForm(formName, callback) {
                this.$refs[formName].validate(callback);
            },

        },
        mounted() {
            this.memberListPage()
        },
        created() {
            this.addForm = {}
            this.updateForm = {}
            this.addForm = _.cloneDeep(formObj)
            this.updateForm = _.cloneDeep(formObj)
        }
    };
</script>

<style>
    .page {
        padding: 10px;
    }
</style>